package com.fourth_groups.shopping121store.ui.cart.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;

import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.bean.GoodsHotSaleBean;
import com.fourth_groups.shopping121store.http.OkHttpClientUtils;
import com.fourth_groups.shopping121store.ui.MainActivity;
import com.fourth_groups.shopping121store.ui.cart.adapter.CartHotSaleAdapter;
import com.fourth_groups.shopping121store.utils.GsonImpl;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HotSaleActivity_Cart extends Fragment{

    private String url="http://api.121dian.cn/v2/emptycart?offset=0&limit=10&access_token=bi2Qn6Oi8KJguHAebOaO";

    private Button hotSaleShopping;
    private GridView gvHostsale;
    private CartHotSaleAdapter adapter;
    List<GoodsHotSaleBean.DataBean> mGoodsHotSaleBeens=new ArrayList<>();

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                 mGoodsHotSaleBeens= (List<GoodsHotSaleBean.DataBean>) msg.obj;
                adapter.setData(mGoodsHotSaleBeens);
                gvHostsale.setAdapter(adapter);
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_hot_sale__cart, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView();//初始化控件

        loadData(url);//创建数据

        adapter = new CartHotSaleAdapter();

        hotSaleShopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MainActivity.class));
                adapter.notifyDataSetChanged();

            }
        });
    }


    private void initView() {

        hotSaleShopping = (Button) getActivity().findViewById(R.id.bt_hot_sale_shopping_cart);
        gvHostsale = (GridView) getActivity().findViewById(R.id.gv_hotsale_cart);

    }

    private void loadData(String url) {

        //下载json
        OkHttpClientUtils.getDataAsync(getActivity(), url, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if(response.isSuccessful()){
                    String json = response.body().string();

//                    Log.i("TAG",json);
                    //gson解析
                    GoodsHotSaleBean goodsHotSaleBeen = GsonImpl.get().toObject(json, GoodsHotSaleBean.class);
                    List<GoodsHotSaleBean.DataBean> goodsHotSaleBeens = goodsHotSaleBeen.getData();

                    Message msg=Message.obtain();
                    msg.what=1;
                    msg.obj=goodsHotSaleBeens;
                    mHandler.sendMessage(msg);
                }
            }
        },new Object());

        CartHotSaleAdapter adapter=new CartHotSaleAdapter();
        gvHostsale.setAdapter(adapter);
    }
}
